/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Ripper
 */
public class EditPlayerPanel extends ClientPanel implements ActionListener
{
    private DrawPlayerPanel drawplayerpanel;
    
    private ColorEditingPanel[] colorediting;
    private JButton returnButton;
    
    private int looktype;
    private int maxLooktype = 4;
    
    private JButton nextLookTypeButton;
    private JButton previousLookTypeButton;
    
    private boolean initialised;
    
    public EditPlayerPanel(ClientWindow cw)
    {
        super(cw);
        setLayout(new BorderLayout());
        initialised = false;
        
        drawplayerpanel = new DrawPlayerPanel();
        colorediting = new ColorEditingPanel[4];
        
        JPanel centerPanel = new JPanel(new BorderLayout());
        {
            centerPanel.add(drawplayerpanel, BorderLayout.CENTER);
            
            JPanel buttonz = new JPanel(new GridLayout(2,1));
            {
                JPanel changeLookTypePanel = new JPanel(new GridLayout(1,2));
                {
                    previousLookTypeButton = new JButton(" < ");
                    previousLookTypeButton.addActionListener(this);
                    changeLookTypePanel.add(previousLookTypeButton);
                    nextLookTypeButton = new JButton(" > ");
                    nextLookTypeButton.addActionListener(this);
                    changeLookTypePanel.add(nextLookTypeButton);
                }
                buttonz.add(changeLookTypePanel);
                
                returnButton = new JButton("    Save & Exit    ");
                returnButton.addActionListener(this);
                buttonz.add(returnButton);
            }
            centerPanel.add(buttonz, BorderLayout.SOUTH);
        }
        add(centerPanel, BorderLayout.CENTER);
        JPanel easternPanel = new JPanel(new BorderLayout());
        {
            JPanel eastPanel = new JPanel(new GridLayout(4,1));
            {
                for(int a = 0;a < colorediting.length; a++)
                {
                    colorediting[a] = new ColorEditingPanel(a);
                    eastPanel.add(colorediting[a]);
                }
            }
            easternPanel.add(eastPanel, BorderLayout.NORTH);
        }
        add(easternPanel, BorderLayout.EAST);
    }
    
    public void adjustLookType(int d)
    {
        looktype += d;
        if(looktype > maxLooktype)
        {
            looktype = 1;
        }
        else if(looktype < 1)
        {
            looktype = maxLooktype;
        }
        
        getClientWindow().setNewLooktype(looktype);
            Color[] colors = {colorediting[0].getColor(), 
                              colorediting[1].getColor(), 
                              colorediting[2].getColor(), 
                              colorediting[3].getColor()};
        fillWithData(looktype, colors);
        drawplayerpanel.repaint();
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == returnButton)
        {
            Color[] colors = {colorediting[0].getColor(), 
                              colorediting[1].getColor(), 
                              colorediting[2].getColor(), 
                              colorediting[3].getColor()};
            getClientWindow().changeColors(colors);
            
            getClientWindow().gotoWelcomeScreen();
        }
        else if(e.getSource() == nextLookTypeButton)
        {
            adjustLookType(1);
        }
        else if(e.getSource() == previousLookTypeButton)
        {
            adjustLookType(-1);
        }
    }
    
    public void fillWithData(int looktype, Color[] colors)
    {
        this.looktype = looktype;
        drawplayerpanel.setData(looktype, colors, getClientWindow().getClientDomainController());
        for(int a = 0;a < colorediting.length; a++)
        {
            colorediting[a].setColor(colors[a]);
        }
        initialised = true;
    }
    
    private class ColorEditingPanel extends JPanel
    {
        private Color color;
        private int colorIndex;
        
        private JLabel indexLabel;
        private JPanel colorPreviewPanel;
        private JSlider redSlider;
        private JSlider greenSlider;
        private JSlider blueSlider;
        
        private ChangeListingGuy changeListener;
        
        public ColorEditingPanel(int colorIndex)
        {
            this.colorIndex = colorIndex;
            
            setLayout(new BorderLayout());
            
            JPanel centerPanel = new JPanel(new BorderLayout());
            {
                indexLabel = new JLabel("  Color "+(colorIndex+1)+":   ");
                centerPanel.add(indexLabel, BorderLayout.NORTH);
                
                colorPreviewPanel = new JPanel();
                centerPanel.add(colorPreviewPanel, BorderLayout.CENTER);
            }
            this.add(centerPanel, BorderLayout.CENTER);
            
            JPanel easternPanel = new JPanel(new BorderLayout());
            {
                JPanel coloryThings = new JPanel(new GridLayout(3,1));
                {
                    changeListener = new ChangeListingGuy();
                    JPanel redcontainmentpanel = new JPanel(new BorderLayout());
                    {
                        JLabel cowor = new JLabel(" Red: ");
                        redcontainmentpanel.add(cowor, BorderLayout.WEST);
                        redSlider = new JSlider(0, 255);
                        redSlider.addChangeListener(changeListener);
                        redcontainmentpanel.add(redSlider, BorderLayout.CENTER);
                    }
                    coloryThings.add(redcontainmentpanel);
                    JPanel greencontainmentpanel = new JPanel(new BorderLayout());
                    {
                        greencontainmentpanel.add(new JLabel(" Green: "), BorderLayout.WEST);
                        greenSlider = new JSlider(0,255);
                        greenSlider.addChangeListener(changeListener);
                        greencontainmentpanel.add(greenSlider, BorderLayout.CENTER);
                    }
                    coloryThings.add(greencontainmentpanel);
                    JPanel bluecontainmentpanel = new JPanel(new BorderLayout());
                    {
                        bluecontainmentpanel.add(new JLabel(" Blue: "), BorderLayout.WEST);
                        blueSlider = new JSlider(0,255);
                        blueSlider.addChangeListener(changeListener);
                        bluecontainmentpanel.add(blueSlider, BorderLayout.CENTER);
                    }
                    coloryThings.add(bluecontainmentpanel);
                }
                easternPanel.add(coloryThings, BorderLayout.NORTH);
            }
            this.add(easternPanel, BorderLayout.EAST);
            
            setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }
        public int getColorIndex() { return colorIndex; }
        
        public void setColor(Color c)
        {
            color = c;
            colorPreviewPanel.setBackground(c);
            
            redSlider.setValue(c.getRed());
            greenSlider.setValue(c.getGreen());
            blueSlider.setValue(c.getBlue());
        }
        public Color getColor() { return color; }
        
        private class ChangeListingGuy implements ChangeListener
        {
            @Override
            public void stateChanged(ChangeEvent e) 
            {
                if(initialised)
                {
                    //int r = Integer.parseInt(redTextField.getText());
                    int r = redSlider.getValue();
                    int g = greenSlider.getValue();
                    int b = blueSlider.getValue();

                    Color newColor = new Color(r,g,b);
                    setColor(newColor);

                    Color[] cols = {colorediting[0].getColor(), colorediting[1].getColor(),
                                    colorediting[2].getColor(), colorediting[3].getColor()};

                    fillWithData(looktype, cols);
                    drawplayerpanel.repaint();
                }
            }
            
        }
    }
}
